#include "boat.hpp"

#include "stage.hpp"

static const float WIDTH = 16;
static const float HEIGHT = 2;
static const float VOLUME = WIDTH * HEIGHT;

void Boat::Init(float x, float y, Cue & cue)
{
	body = Box2f(Vec2f(-WIDTH*.5f, -HEIGHT*.5f), Vec2f(WIDTH, HEIGHT));
	pos = Vec2f(x, y);
	hp = 0;
}

void Boat::In()
{
}

void Boat::Update()
{
	const float FALL_ACCEL = 1.f / 32;
	const float FALL_TERMVEL = 1.f;
	const float FLOAT_ACCEL_PER_WATER = -FALL_ACCEL / 4;
	const float FLOAT_TERMVEL = -.25f;

	vel.y += FALL_ACCEL;
	vel.y = std::min(FALL_TERMVEL, vel.y);

	float watervol = Stg.PropArea(Stage::WATER, body, pos + vel);

	vel.y += FLOAT_ACCEL_PER_WATER * watervol;
	vel.y = std::max(FLOAT_TERMVEL, vel.y);

	for (int ax = Y; ax > NOAXIS; --ax)
	{
		pos[ax] += vel[ax];
		stghit[ax] = Stg.AxisHit(body, pos, ax);
		if (StgHitting(ax))
			pos[ax] -= stghit[ax];
	}
}

ActPool<Boat, Boat::COUNT> Boats;
